<?php
$directory="./img/bg/";
$work= $_SERVER['DOCUMENT_ROOT']."/". $directory;
//echo $work;
$dir=opendir($work);

$pattern="\.(gif|jpg|jpeg|png|bmp|swf)$";
if(!$dir)
{
die("Failed to read directory");
}
$s=readdir($dir);
$count="0";
$image;
while($s)
{
if(ereg($pattern, $s))
{
$image[$count]=$s;
$count++;
}
$s=readdir($dir);
}
closedir($dir);

$limit=count($image);
$limit--;
$randNum=rand(0,$limit);
	$ranim=$work.$image[$randNum]; 
$ranimage= imagecreatefromjpeg($ranim);
 //echo $ranim;
 
header("Content-Type: image/jpeg");
header ("cache-control: must-revalidate"); 
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Cache-Control: no-cache");
header("Pragma: no-cache");
header("Content-Transfer-Encoding: binary");
 imagejpeg($ranimage);
 
//header("Content-Type: image/jpeg");
//header("Content-Transfer-Encoding: binary");
// header("Location: ". $work.$image[$randNum]);
// echo($work.$image[$randNum]);
?>